package com.hsw.bestof5;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hsw.CalcTest.R;

public class BestOf5Activity extends Activity {
	int my_maxRandom=100;
	int my_allRound;
	int my_goodRound;
	int my_firstNumber;
	int my_secondNumber;
	int my_thirdNumber;
	int my_failNr;
	String my_operator;
	String my_functionType;
	String my_replaceOrder;
	private OnClickListener mResetListener = new OnClickListener() {
	    public void onClick(View v) {
	    	DoReset();
	    }
	};
    /** Called when the activity is first created. */

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main); 
        InitializeButtonSize(); 
        InitializeResult();
        ShowResult();
        GiveNextRound();
        Button button = (Button)findViewById(R.id.btReset);
        button.setOnClickListener(mResetListener);

    }
    
    public void DoReset(){
    	my_allRound=0;
    	my_goodRound=0;
    	ShowResult();
    	GiveNextRound();
    }
    
    public void ShowResult(){
    	TextView tvGoodAnswer = (TextView)findViewById(R.id.tvGoodAnswer);
    	String txtGood = Integer.toString(my_goodRound);
    	tvGoodAnswer.setText(txtGood);
    	
    	TextView tvAllAnswer = (TextView)findViewById(R.id.tvAllAnswer);
    	String txtAll = Integer.toString(my_allRound);
    	tvAllAnswer.setText(txtAll);
    	
    	int percentResult=0;
    	if (my_allRound!=0) {
			percentResult=my_goodRound*100/my_allRound;
		}
    	TextView tvPercentResult = (TextView)findViewById(R.id.tvResultPercent);
    	String txtPercent = Integer.toString(percentResult);
    	tvPercentResult.setText(txtPercent);
    }
    
    public void GiveNextRound(){
    	//calculate random 
    	int rndOperator = new Random().nextInt(2);
    	if (rndOperator==0) {
			my_operator="Summation";
		}
    	int rndFunctionType = new Random().nextInt(2);
    	if (rndFunctionType==0) {
    		my_functionType="Replacement";
		}
    	int rndReplaceOrder = new Random().nextInt(2);
    	if (rndReplaceOrder==0) {
    		my_replaceOrder="1st";
		}
    	
    	int nr1 = new Random().nextInt(my_maxRandom) + 1;
    	int nr2 = new Random().nextInt(my_maxRandom) + 1;
    	
    	//calculate position
    	if (my_functionType=="Replacement") {
    		if (my_operator=="Summation") {
    			if (my_replaceOrder=="1st") {
				
    			}
    		}
    	}
    	else {
			if (my_operator=="Summation") {
				if ((nr1+nr2)>my_maxRandom) {
				    if (nr1>nr2) {
				    	my_firstNumber=nr2;
				    	my_secondNumber=nr1-nr2;;
				    }
				    else {
				    	my_firstNumber=nr2;
				    	my_secondNumber=nr1;
					}
				}
			}
			else {
				if (nr1>nr2) {
					my_firstNumber=nr1;
					my_secondNumber=nr2;
				}
				else {
					my_firstNumber=nr2;
					my_secondNumber=nr1;
				}
			}
			my_failNr=3;
		}
    	
    	//show the start
    	ShowFunction();
    }
    
    public void ShowFunction(){
    	switch (my_failNr) {
    	case 1:{
        	TextView tv2 = (TextView)findViewById(R.id.tvSecondNumber);
        	String txt2 = Integer.toString(my_secondNumber);
        	tv2.setText(txt2);
        	tv2.setTextColor(Color.WHITE);
        	TextView tv3 = (TextView)findViewById(R.id.tvThirdNumber);
        	String txt3 = Integer.toString(my_thirdNumber);
        	tv3.setText(txt3);
        	tv3.setTextColor(Color.WHITE);
        	
        	TextView tv1 = (TextView)findViewById(R.id.tvFirstNumber);
        	tv1.setText("_");
        	tv1.setTextColor(Color.GREEN);  	
    	}break;
    	case 2:{
        	TextView tv1 = (TextView)findViewById(R.id.tvFirstNumber);
        	String txt1 = Integer.toString(my_firstNumber);
        	tv1.setText(txt1);
        	tv1.setTextColor(Color.WHITE);
        	TextView tv3 = (TextView)findViewById(R.id.tvThirdNumber);
        	String txt3 = Integer.toString(my_thirdNumber);
        	tv3.setText(txt3);
        	tv3.setTextColor(Color.WHITE);

        	TextView tv2 = (TextView)findViewById(R.id.tvSecondNumber);
        	tv2.setText("_");
        	tv2.setTextColor(Color.GREEN);
    	}break;
    	case 3:{
        	TextView tv1 = (TextView)findViewById(R.id.tvFirstNumber);
        	String txt1 = Integer.toString(my_firstNumber);
        	tv1.setText(txt1);
        	tv1.setTextColor(Color.WHITE);
        	TextView tv2 = (TextView)findViewById(R.id.tvSecondNumber);
        	String txt2 = Integer.toString(my_secondNumber);
        	tv2.setText(txt2);
        	tv2.setTextColor(Color.WHITE);

        	
        	TextView tv3 = (TextView)findViewById(R.id.tvThirdNumber);
        	tv3.setText("_");
        	tv3.setTextColor(Color.GREEN);
    	}break;
		}
    }
    
    public void InitializeResult(){
    	my_allRound=4;
    	my_goodRound=1;
    }
    
    public void InitializeButtonSize(){
    	Display display = getWindowManager().getDefaultDisplay();
    	int DisplaysizeWith = display.getWidth();
    	int buttonHeight = (DisplaysizeWith )/4;    	
    	int buttonWidth = (DisplaysizeWith )/3;
        
    	LinearLayout ll = (LinearLayout)findViewById(R.id.llKeyboard);
    	ViewGroup.LayoutParams params = ll.getLayoutParams();
    	params.width = DisplaysizeWith;
    	params.height = DisplaysizeWith;
    	ll.setLayoutParams(params);
    	
    	ResizeButton((LinearLayout)findViewById(R.id.llButton0), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton1), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton2), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton3), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton4), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton5), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton6), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton7), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton8), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButton9), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButtonOK), buttonWidth, buttonHeight);
    	ResizeButton((LinearLayout)findViewById(R.id.llButtonBack), buttonWidth, buttonHeight);
    	int textSize = (DisplaysizeWith )/16; 
    	ResizeButtonText((Button)this.findViewById(R.id.bt0), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt1), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt2), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt3), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt4), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt5), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt6), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt7), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt8), textSize);
    	ResizeButtonText((Button)this.findViewById(R.id.bt9), textSize);
    	TextView tv1 = (TextView)findViewById(R.id.tvFirstNumber);
    	tv1.setTextSize(textSize);
    	TextView tv2 = (TextView)findViewById(R.id.tvSecondNumber);
    	tv2.setTextSize(textSize);
    	TextView tv3 = (TextView)findViewById(R.id.tvThirdNumber);
    	tv3.setTextSize(textSize);
    	TextView tv4 = (TextView)findViewById(R.id.tvOperator);
    	tv4.setTextSize(textSize);
    	TextView tv5 = (TextView)findViewById(R.id.tvOperatorEqual);
    	tv5.setTextSize(textSize);
    }
    
    public void ResizeButton(LinearLayout in_ll, int in_width, int in_height){
    	ViewGroup.LayoutParams params = in_ll.getLayoutParams();
    	params = in_ll.getLayoutParams();
    	params.width = in_width;
    	params.height = in_height;
    	in_ll.setLayoutParams(params);
    }
    
    private void ResizeButtonText(Button in_bt, int in_size) {
	    in_bt.setTextSize(in_size);
	}
}